﻿using System;
using System.ComponentModel;
using System.Data;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;

namespace ZKHY.BUSSINESS.PLUGIN
{
	// Token: 0x02000003 RID: 3
	[HotUpdate]
	[Description("差旅费报销 表单插件")]
	public class CLBXFEdit : AbstractBillPlugIn
	{
		// Token: 0x06000004 RID: 4 RVA: 0x0000288C File Offset: 0x00000A8C
		public override void AfterCreateNewEntryRow(CreateNewEntryEventArgs e)
		{
			base.AfterCreateNewEntryRow(e);
			bool flag = StringUtils.EqualsIgnoreCase(e.Entity.Key, "FEntity");
			if (flag)
			{
				int num = Convert.ToInt32(base.View.Model.GetValue("FTravelSeq", e.Row));
				object value = base.View.Model.GetValue("F_UNW_KSSJ", num - 1);
				DateTime dateTime;
				bool flag2;
				if (value is DateTime)
				{
					dateTime = (DateTime)value;
					flag2 = true;
				}
				else
				{
					flag2 = false;
				}
				bool flag3 = flag2;
				if (flag3)
				{
					base.View.Model.SetValue("F_BDI_KSSJ", dateTime, e.Row);
				}
				value = base.View.Model.GetValue("F_UNW_JSSJ", num - 1);
				DateTime dateTime2;
				bool flag4;
				if (value is DateTime)
				{
					dateTime2 = (DateTime)value;
					flag4 = true;
				}
				else
				{
					flag4 = false;
				}
				bool flag5 = flag4;
				if (flag5)
				{
					base.View.Model.SetValue("F_BDI_JSSJ", dateTime2, e.Row);
				}
				decimal num2 = Convert.ToDecimal(base.View.Model.GetValue("F_UNW_FGZSJCCTS", num - 1));
				decimal num3 = Convert.ToDecimal(base.View.Model.GetValue("F_UNW_SC", num - 1));
				base.View.Model.SetValue("F_UNW_FGZSJ", num2, e.Row);
				base.View.Model.SetValue("F_BDI_SC", num3, e.Row);
			}
		}

		// Token: 0x06000005 RID: 5 RVA: 0x00002A18 File Offset: 0x00000C18
		public override void DataChanged(DataChangedEventArgs e)
		{
			base.DataChanged(e);
			string key = e.Field.Key;
			bool flag = key.Equals("F_UNW_JSSJ") || key.Equals("F_UNW_KSSJ");
			if (flag)
			{
				object value = base.View.Model.GetValue("F_UNW_KSSJ", e.Row);
				DateTime dateTime;
				bool flag2;
				if (value is DateTime)
				{
					dateTime = (DateTime)value;
					flag2 = true;
				}
				else
				{
					flag2 = false;
				}
				bool flag3 = flag2;
				if (flag3)
				{
					value = base.View.Model.GetValue("F_UNW_JSSJ", e.Row);
					DateTime dateTime2;
					bool flag4;
					if (value is DateTime)
					{
						dateTime2 = (DateTime)value;
						flag4 = true;
					}
					else
					{
						flag4 = false;
					}
					bool flag5 = flag4;
					if (flag5)
					{
						string text = string.Format("/*dialect*/select fday\r\n                            from T_ENG_WORKCAL t\r\n                            join T_ENG_WorkCalData t1 on t.fid = t1.FID\r\n                            where t.FNUMBER = 'WTC000001' and FDay between '{0}' and '{1}'\r\n                            and FDateStyle = 1\r\n                            order by FDAY", new DateTime(dateTime.Year, dateTime.Month, dateTime.Day), new DateTime(dateTime2.Year, dateTime2.Month, dateTime2.Day));
						DynamicObjectCollection dynamicObjectCollection = DBUtils.ExecuteDynamicObject(base.Context, text, null, null, CommandType.Text, Array.Empty<SqlParam>());
						double num = 0.0;
						bool flag6 = dynamicObjectCollection.Count == 1;
						if (flag6)
						{
							num = this.GetSpanOneDay(dateTime, dateTime2);
							bool flag7 = num < 4.0;
							if (flag7)
							{
								num = 4.25;
							}
							else
							{
								num = 8.5;
							}
						}
						else
						{
							for (int i = 0; i < dynamicObjectCollection.Count; i++)
							{
								DateTime beg = Convert.ToDateTime(dynamicObjectCollection[i]["fday"]);
								bool flag8 = i == 0;
								double num2;
								if (flag8)
								{
									num2 = this.GetSpanStart(dateTime);
								}
								else
								{
									bool flag9 = i == dynamicObjectCollection.Count - 1;
									if (flag9)
									{
										num2 = this.GetSpanEnd(dateTime2);
									}
									else
									{
										num2 = this.GetSpan(beg);
									}
								}
								bool flag10 = i == 0 && num2 < 4.0;
								if (flag10)
								{
									num += 4.25;
								}
								else
								{
									bool flag11 = i == 0 && num2 >= 4.0;
									if (flag11)
									{
										num += 8.5;
									}
									else
									{
										num += num2;
									}
								}
							}
						}
						base.View.Model.SetValue("F_UNW_SC", num / 8.5, e.Row);
						int entryRowCount = base.View.Model.GetEntryRowCount("FEntity");
						for (int j = 0; j < entryRowCount; j++)
						{
							int num3 = Convert.ToInt32(base.View.Model.GetValue("FTravelSeq", j));
							bool flag12 = num3 == e.Row + 1;
							if (flag12)
							{
								base.View.Model.SetValue("F_BDI_KSSJ", dateTime, j);
								base.View.Model.SetValue("F_BDI_JSSJ", dateTime2, j);
								base.View.Model.SetValue("F_BDI_SC", num / 8.5, j);
							}
						}
					}
				}
			}
			bool flag13 = key.Equals("F_UNW_FGZSJCCTS");
			if (flag13)
			{
				int entryRowCount2 = base.View.Model.GetEntryRowCount("FEntity");
				for (int k = 0; k < entryRowCount2; k++)
				{
					int num4 = Convert.ToInt32(base.View.Model.GetValue("FTravelSeq", k));
					bool flag14 = num4 == e.Row + 1;
					if (flag14)
					{
						base.View.Model.SetValue("F_UNW_FGZSJ", e.NewValue, k);
					}
				}
			}
			bool flag15 = key.Equals("F_BDI_KSSJ") || key.Equals("F_BDI_JSSJ");
			if (flag15)
			{
				object value = base.View.Model.GetValue("F_BDI_KSSJ", e.Row);
				DateTime beg2;
				bool flag16;
				if (value is DateTime)
				{
					beg2 = (DateTime)value;
					flag16 = true;
				}
				else
				{
					flag16 = false;
				}
				bool flag17 = flag16;
				if (flag17)
				{
					value = base.View.Model.GetValue("F_BDI_JSSJ", e.Row);
					DateTime dateTime3;
					bool flag18;
					if (value is DateTime)
					{
						dateTime3 = (DateTime)value;
						flag18 = true;
					}
					else
					{
						flag18 = false;
					}
					bool flag19 = flag18;
					if (flag19)
					{
						string text2 = string.Format("/*dialect*/select fday\r\n                            from T_ENG_WORKCAL t\r\n                            join T_ENG_WorkCalData t1 on t.fid = t1.FID\r\n                            where t.FNUMBER = 'WTC000001' and FDay between '{0}' and '{1}'\r\n                            and FDateStyle = 1\r\n                            order by FDAY", new DateTime(beg2.Year, beg2.Month, beg2.Day), new DateTime(dateTime3.Year, dateTime3.Month, dateTime3.Day));
						DynamicObjectCollection dynamicObjectCollection2 = DBUtils.ExecuteDynamicObject(base.Context, text2, null, null, CommandType.Text, Array.Empty<SqlParam>());
						double num5 = 0.0;
						bool flag20 = dynamicObjectCollection2.Count == 1;
						if (flag20)
						{
							num5 = this.GetSpanOneDay(beg2, dateTime3);
							bool flag21 = num5 < 4.0;
							if (flag21)
							{
								num5 = 4.25;
							}
							else
							{
								num5 = 8.5;
							}
						}
						else
						{
							for (int l = 0; l < dynamicObjectCollection2.Count; l++)
							{
								DateTime beg3 = Convert.ToDateTime(dynamicObjectCollection2[l]["fday"]);
								bool flag22 = l == 0;
								double num6;
								if (flag22)
								{
									num6 = this.GetSpanStart(beg2);
								}
								else
								{
									bool flag23 = l == dynamicObjectCollection2.Count - 1;
									if (flag23)
									{
										num6 = this.GetSpanEnd(dateTime3);
									}
									else
									{
										num6 = this.GetSpan(beg3);
									}
								}
								bool flag24 = l == 0 && num6 < 4.0;
								if (flag24)
								{
									num5 += 4.25;
								}
								else
								{
									bool flag25 = l == 0 && num6 >= 4.0;
									if (flag25)
									{
										num5 += 8.5;
									}
									else
									{
										num5 += num6;
									}
								}
							}
						}
						base.View.Model.SetValue("F_BDI_SC", num5 / 8.5, e.Row);
					}
				}
			}
		}

		// Token: 0x06000006 RID: 6 RVA: 0x0000309C File Offset: 0x0000129C
		public double GetSpan(DateTime beg)
		{
			bool flag = beg.DayOfWeek == DayOfWeek.Saturday;
			double num;
			if (flag)
			{
				num = this.CalculateOverlap(beg, new DateTime(beg.Year, beg.Month, beg.Day, 12, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 12, 30, 0)).TotalHours;
				bool flag2 = num == 4.0;
				if (flag2)
				{
					num = 4.25;
				}
			}
			else
			{
				num = this.CalculateOverlap(beg, new DateTime(beg.Year, beg.Month, beg.Day, 17, 0, 0), new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 17, 0, 0)).TotalHours;
			}
			return num;
		}

		// Token: 0x06000007 RID: 7 RVA: 0x000031C4 File Offset: 0x000013C4
		public double GetSpanStart(DateTime beg)
		{
			bool flag = beg.DayOfWeek == DayOfWeek.Saturday;
			double num;
			if (flag)
			{
				num = this.CalculateOverlap(beg, new DateTime(beg.Year, beg.Month, beg.Day, 12, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 12, 30, 0)).TotalHours;
				bool flag2 = num == 4.0;
				if (flag2)
				{
					num = 4.25;
				}
			}
			else
			{
				num = this.CalculateOverlap(beg, new DateTime(beg.Year, beg.Month, beg.Day, 17, 0, 0), new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 17, 0, 0)).TotalHours;
			}
			return num;
		}

		// Token: 0x06000008 RID: 8 RVA: 0x000032EC File Offset: 0x000014EC
		public double GetSpanEnd(DateTime beg)
		{
			bool flag = beg.DayOfWeek == DayOfWeek.Saturday;
			double num;
			if (flag)
			{
				num = this.CalculateOverlap(new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), beg, new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 12, 30, 0)).TotalHours;
				bool flag2 = num == 4.0;
				if (flag2)
				{
					num = 4.25;
				}
			}
			else
			{
				num = this.CalculateOverlap(new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), beg, new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 17, 0, 0)).TotalHours;
			}
			return num;
		}

		// Token: 0x06000009 RID: 9 RVA: 0x00003414 File Offset: 0x00001614
		public double GetSpanOneDay(DateTime beg, DateTime end)
		{
			bool flag = beg.DayOfWeek == DayOfWeek.Saturday;
			double num;
			if (flag)
			{
				num = this.CalculateOverlap(beg, end, new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 12, 30, 0)).TotalHours;
				bool flag2 = num == 4.0;
				if (flag2)
				{
					num = 4.25;
				}
			}
			else
			{
				num = this.CalculateOverlap(beg, end, new DateTime(beg.Year, beg.Month, beg.Day, 8, 30, 0), new DateTime(beg.Year, beg.Month, beg.Day, 17, 0, 0)).TotalHours;
			}
			return num;
		}

		// Token: 0x0600000A RID: 10 RVA: 0x00003500 File Offset: 0x00001700
		public TimeSpan CalculateOverlap(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
		{
			bool flag = start1 > end1;
			if (flag)
			{
				DateTime dateTime = start1;
				start1 = end1;
				end1 = dateTime;
			}
			bool flag2 = start2 > end2;
			if (flag2)
			{
				DateTime dateTime2 = start2;
				start2 = end2;
				end2 = dateTime2;
			}
			DateTime dateTime3 = (start1 > start2) ? start1 : start2;
			DateTime dateTime4 = (end1 < end2) ? end1 : end2;
			bool flag3 = dateTime3 > dateTime4;
			TimeSpan result;
			if (flag3)
			{
				result = TimeSpan.Zero;
			}
			else
			{
				result = dateTime4 - dateTime3;
			}
			return result;
		}
	}
}
